package com.zhh.front.controller;

import com.zhh.api.model.RechargeRecord;
import com.zhh.front.view.RespResult;
import com.zhh.front.view.center.RechargeResultView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "充值业务")
@RestController
@RequestMapping("/v1/recharge")
public class RechargeController extends BaseController{

    //获取用户和资金信息
    @ApiOperation(value = "获取用户和资金信息")
    @GetMapping("/records")
    public RespResult queryRechargePage(@RequestHeader("uid") Integer uid,
                                        @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                                        @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        RespResult result = RespResult.fail();

        //数据验证
        if(uid != null && uid > 0) {
            //数据库中取出充值记录
            List<RechargeRecord> records = rechargeService.queryByUid(uid,pageNo,pageSize);
            //成功
            result = RespResult.ok();
            //把RechargeRecord类转换成RechargeResultView视图类
            result.setList(toView(records));
        }
        return result;
    }


    private List<RechargeResultView> toView(List<RechargeRecord> src){
        List<RechargeResultView> target = new ArrayList<>();
        src.forEach( record -> {
            target.add( new RechargeResultView(record));
        });
        return target;
    }
}
